package com.example.handpose_front.DictionarySelectPage;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.handpose_front.BaseActivity;
import com.example.handpose_front.R;
import com.example.handpose_front.dto.DataResponse;
import com.example.handpose_front.dto.HandDictionaryDTO;
import com.example.handpose_front.utils.LocalStorage;
import com.example.handpose_front.utils.OKHttpUtil.HttpUrl;
import com.example.handpose_front.utils.OKHttpUtil.OKHttpUtil;
import com.example.handpose_front.utils.OKHttpUtil.ResponseBody;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class DictionarySelectActivity extends BaseActivity implements View.OnClickListener {

    // 词典选择
    private View chineseDictionaryTV;
    private View usaDictionaryTV;
    private View frenchDictionaryTV;

    // 词典信息
    private TextView chineseDictionaryName;
    private TextView usaDictionaryName;
    private TextView frenchDictionaryName;
    private TextView chineseDictionaryWords;
    private TextView usaDictionaryWords;
    private TextView frenchDictionaryWords;
    private LinearLayout chinsesDicStudying;
    private LinearLayout usaDicStudying;
    private LinearLayout frenchDicStudying;

    // 响应体
    private DataResponse dataResponse;

    private Map<String, String> map = new HashMap<>();
    public String userPhone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dictionary_select);
        initView();
        initEvent();
    }

    @Override
    public void onClick(View v) {

        // 当前用户手机号、当前选择词典id、之前学习过的词典id
        String userPhone_send = LocalStorage.getCurrentPhone();
        System.out.println(userPhone_send);
        String curDictionaryId = "";
        if (v.getId() == R.id.chinese_dictionary_tv) {
            curDictionaryId = String.valueOf(1);
        }
        if (v.getId() == R.id.usa_dictionary_tv) {
            curDictionaryId = String.valueOf(2);
        }
        if (v.getId() == R.id.french_dictionary_tv) {
            curDictionaryId = String.valueOf(3);
        }

        // 向后端发起请求
        if (LocalStorage.getLocalCurDictionaryId().equals(curDictionaryId)) {
            Toast.makeText(this, "当前正在学习该词书哦~", Toast.LENGTH_SHORT).show();
        } else {
            userPhone = LocalStorage.getCurrentPhone();
            if (sendSetCurDictionaryRequest(userPhone, curDictionaryId)) {
                Toast.makeText(this, "开始学习该词书~", Toast.LENGTH_SHORT).show();
                // LocalStorage更新
                LocalStorage.setCurDictionaryId(curDictionaryId);
            }
        }

    }

    // 设置当前学习词典的学习位置为1，把之前学习的词典的学习位置为0
    public boolean sendSetCurDictionaryRequest(String userPhone, String curDictionaryId) {
        map.clear();

        map.put("userPhone", userPhone);
        map.put("curDictionaryId", curDictionaryId);

        String response = OKHttpUtil.postSyncRequest(HttpUrl.getBaseUrl(), map, "curDictionary", "set_cur");
        dataResponse = ResponseBody.getResponseBody(response);
        Log.d("curDictionaryId：", dataResponse.getData().get("curDictionaryId").toString());

        if (dataResponse.getCode().equals("200")) {
            // 成功设置词典
            return true;
        } else {
            // 提示错误信息
            Toast.makeText(this, "操作失败", Toast.LENGTH_SHORT).show();
            return false;
        }

    }

    // 获取全部词典信息
    public boolean sendGetHandDictionaryRequest() {

        String response = OKHttpUtil.postSyncRequest(HttpUrl.getBaseUrl(), map, "handDictionary", "get_dics");
        dataResponse = ResponseBody.getResponseBody(response);
        if (dataResponse.getCode().equals("200")) {
            // 成功获取全部词典

            Map<String, String> responseMap = dataResponse.getData();
            Set<String> keys = responseMap.keySet();

            for (String key : keys) {
                // 将json字符串转换回handDictionaryDTO
                Gson gson = new Gson();
                HandDictionaryDTO handDictionaryDTO = null;
                handDictionaryDTO = gson.fromJson(responseMap.get(key), HandDictionaryDTO.class);
                String id = handDictionaryDTO.getHandDictionaryId().toString();
                System.out.println(id);
                if (id.equals("1")) {
                    chineseDictionaryName.setText(handDictionaryDTO.getHandDictionaryName().toString());
                    chineseDictionaryWords.setText(handDictionaryDTO.getHandDictionaryWords().toString() + "词");
                }
                if (id.equals("2")) {
                    usaDictionaryName.setText(handDictionaryDTO.getHandDictionaryName());
                    usaDictionaryWords.setText(handDictionaryDTO.getHandDictionaryWords().toString() + "词");
                }
                if (id.equals("3")) {
                    frenchDictionaryName.setText(handDictionaryDTO.getHandDictionaryName());
                    frenchDictionaryWords.setText(handDictionaryDTO.getHandDictionaryWords().toString() + "词");
                }
            }
            return true;
        } else {
            // 提示错误信息
            Toast.makeText(this, "操作失败", Toast.LENGTH_SHORT).show();
            return false;
        }
    }

    private void initView() {
        // 初始化
        chineseDictionaryTV = findViewById(R.id.chinese_dictionary_tv);
        usaDictionaryTV = findViewById(R.id.usa_dictionary_tv);
        frenchDictionaryTV = findViewById(R.id.french_dictionary_tv);
        chineseDictionaryName = findViewById(R.id.chinese_dic_name);
        usaDictionaryName = findViewById(R.id.usa_dic_name);
        frenchDictionaryName = findViewById(R.id.french_dic_name);
        chineseDictionaryWords = findViewById(R.id.chinese_dic_words);
        usaDictionaryWords = findViewById(R.id.usa_dic_words);
        frenchDictionaryWords = findViewById(R.id.french_dic_words);
        chinsesDicStudying = (LinearLayout)findViewById(R.id.chinese_dic_studying);
        usaDicStudying = (LinearLayout)findViewById(R.id.usa_dic_studying);
        frenchDicStudying = (LinearLayout)findViewById(R.id.french_dic_studying);
        sendGetHandDictionaryRequest();
    }

    private void initEvent() {
        // 设置点击事件
        chineseDictionaryTV.setOnClickListener(this);
        usaDictionaryTV.setOnClickListener(this);
        frenchDictionaryTV.setOnClickListener(this);
    }

}